/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvFormation;
import fr.cggveritas.crewintranet.jms.entities.CvTypeformation;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypeformationJpaController implements Serializable {

    public CvTypeformationJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvTypeformation cvTypeformation) {
        if (cvTypeformation.getCvFormationList() == null) {
            cvTypeformation.setCvFormationList(new ArrayList<CvFormation>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvFormation> attachedCvFormationList = new ArrayList<CvFormation>();
            for (CvFormation cvFormationListCvFormationToAttach : cvTypeformation.getCvFormationList()) {
                cvFormationListCvFormationToAttach = em.getReference(cvFormationListCvFormationToAttach.getClass(), cvFormationListCvFormationToAttach.getIdformation());
                attachedCvFormationList.add(cvFormationListCvFormationToAttach);
            }
            cvTypeformation.setCvFormationList(attachedCvFormationList);
            em.persist(cvTypeformation);
            for (CvFormation cvFormationListCvFormation : cvTypeformation.getCvFormationList()) {
                CvTypeformation oldIdtypeformationOfCvFormationListCvFormation = cvFormationListCvFormation.getIdtypeformation();
                cvFormationListCvFormation.setIdtypeformation(cvTypeformation);
                cvFormationListCvFormation = em.merge(cvFormationListCvFormation);
                if (oldIdtypeformationOfCvFormationListCvFormation != null) {
                    oldIdtypeformationOfCvFormationListCvFormation.getCvFormationList().remove(cvFormationListCvFormation);
                    oldIdtypeformationOfCvFormationListCvFormation = em.merge(oldIdtypeformationOfCvFormationListCvFormation);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypeformation cvTypeformation) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeformation persistentCvTypeformation = em.find(CvTypeformation.class, cvTypeformation.getIdtypeformation());
            List<CvFormation> cvFormationListOld = persistentCvTypeformation.getCvFormationList();
            List<CvFormation> cvFormationListNew = cvTypeformation.getCvFormationList();
            List<String> illegalOrphanMessages = null;
            for (CvFormation cvFormationListOldCvFormation : cvFormationListOld) {
                if (!cvFormationListNew.contains(cvFormationListOldCvFormation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvFormation " + cvFormationListOldCvFormation + " since its idtypeformation field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvFormation> attachedCvFormationListNew = new ArrayList<CvFormation>();
            for (CvFormation cvFormationListNewCvFormationToAttach : cvFormationListNew) {
                cvFormationListNewCvFormationToAttach = em.getReference(cvFormationListNewCvFormationToAttach.getClass(), cvFormationListNewCvFormationToAttach.getIdformation());
                attachedCvFormationListNew.add(cvFormationListNewCvFormationToAttach);
            }
            cvFormationListNew = attachedCvFormationListNew;
            cvTypeformation.setCvFormationList(cvFormationListNew);
            cvTypeformation = em.merge(cvTypeformation);
            for (CvFormation cvFormationListNewCvFormation : cvFormationListNew) {
                if (!cvFormationListOld.contains(cvFormationListNewCvFormation)) {
                    CvTypeformation oldIdtypeformationOfCvFormationListNewCvFormation = cvFormationListNewCvFormation.getIdtypeformation();
                    cvFormationListNewCvFormation.setIdtypeformation(cvTypeformation);
                    cvFormationListNewCvFormation = em.merge(cvFormationListNewCvFormation);
                    if (oldIdtypeformationOfCvFormationListNewCvFormation != null && !oldIdtypeformationOfCvFormationListNewCvFormation.equals(cvTypeformation)) {
                        oldIdtypeformationOfCvFormationListNewCvFormation.getCvFormationList().remove(cvFormationListNewCvFormation);
                        oldIdtypeformationOfCvFormationListNewCvFormation = em.merge(oldIdtypeformationOfCvFormationListNewCvFormation);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvTypeformation.getIdtypeformation();
                if (findCvTypeformation(id) == null) {
                    throw new NonexistentEntityException("The cvTypeformation with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeformation cvTypeformation;
            try {
                cvTypeformation = em.getReference(CvTypeformation.class, id);
                cvTypeformation.getIdtypeformation();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypeformation with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvFormation> cvFormationListOrphanCheck = cvTypeformation.getCvFormationList();
            for (CvFormation cvFormationListOrphanCheckCvFormation : cvFormationListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvTypeformation (" + cvTypeformation + ") cannot be destroyed since the CvFormation " + cvFormationListOrphanCheckCvFormation + " in its cvFormationList field has a non-nullable idtypeformation field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvTypeformation);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypeformation> findCvTypeformationEntities() {
        return findCvTypeformationEntities(true, -1, -1);
    }

    public List<CvTypeformation> findCvTypeformationEntities(int maxResults, int firstResult) {
        return findCvTypeformationEntities(false, maxResults, firstResult);
    }

    private List<CvTypeformation> findCvTypeformationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypeformation.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypeformation findCvTypeformation(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypeformation.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypeformationCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypeformation> rt = cq.from(CvTypeformation.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
